This is a recurring theme through all of the examples. The only real difference is how we design our code to hit JDBC.
Please see the notes on this example below the code.
Class Relationships
Important Note
All of these examples are configured to use hsqldb which is a free database that supports both in-memory operation as well as using the disk (and other configurations). I've set it up to work strictly in memory. This means:
Every time an application starts, we need to re-create the schema
We lose all of the data at exit
Since this is NOT a JDBC example but rather I use JDBC as a vehicle to better understand the design behind the Spring templates, I figure this is a reasonable solution.
JdbcExample.java
01: packageaaa.valtech.jug.version1;
02:
03: importjava.sql.Connection;
04: importjava.sql.PreparedStatement;
05: importjava.sql.ResultSet;
06: importjava.sql.SQLException;
07: importjava.sql.Statement;
08:
09: importloggingutil.ILogger;10: importloggingutil.LoggingConfiguration;11:
12: importorg.springframework.jdbc.datasource.DriverManagerDataSource;13:
14: importcom.valtech.util.DatabaseUtil;15:
16: publicclass JdbcExample {17: privatestaticfinal ILogger logger = LoggingConfiguration.getLoggerFor(JdbcExample.class);18: privatefinal DriverManagerDataSource dataSource;19:
20: publicstaticfinalString CREATE_TABLE = "CREATE TABLE customer (First_Name char(50), Last_Name char(50))";21:
22: publicstaticfinalString INSERT = "INSERT INTO customer (First_Name, Last_Name) VALUES (?, ?)";23:
24: publicstaticfinalString SELECT_BY_FIRST = "SELECT First_Name, Last_Name from Customer where First_Name = ?";25:
26: public JdbcExample(){27: dataSource = new DriverManagerDataSource();28: dataSource.setDriverClassName("org.hsqldb.jdbcDriver");29: dataSource.setUrl("jdbc:hsqldb:mem:mem:aname");30: dataSource.setUsername("sa");31: dataSource.setPassword("");32: }33:
34: publicvoid installSchema()throwsSQLException{35: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:09 PM36: Statement s = null;// NOPMD by brett.schuchert on 7/11/06 11:09 PM37:
38: try{39: c = dataSource.getConnection();40: s = c.createStatement();41: s.executeUpdate(CREATE_TABLE);42: }finally{43: DatabaseUtil.close(s);44: DatabaseUtil.close(c);45: }46: }47:
48: publicvoid populateTables()throwsSQLException{49: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:09 PM50: PreparedStatement ps = null;51:
52: try{53: c = dataSource.getConnection();54: ps = c.prepareStatement(INSERT);55: insertOneRecord(ps, "Brett", "Schuchert");56: insertOneRecord(ps, "Jeana", "Smith");57: insertOneRecord(ps, "Brett", "Anotherone");58: }finally{59: DatabaseUtil.close(ps);60: DatabaseUtil.close(c);61: }62: }63:
64: privatevoid insertOneRecord(finalPreparedStatement ps, finalString firstName,
65: finalString lastName)throwsSQLException{66: ps.setString(1, firstName);67: ps.setString(2, lastName);68: ps.executeUpdate();69: }70:
71: publicvoid performSearch()throwsSQLException{72: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:09 PM73: PreparedStatement ps = null;74: ResultSet rs = null;// NOPMD by brett.schuchert on 7/11/06 11:09 PM75:
76: try{77: c = dataSource.getConnection();78: ps = c.prepareStatement(SELECT_BY_FIRST);79: ps.setString(1, "Brett");80: rs = ps.executeQuery();81: logger.debug("Records found:");82: while(rs.next()){83: logger.debug("\n\t%s %s", rs.getString(1), rs.getString(2));84: }85: }finally{86: DatabaseUtil.close(rs);87: DatabaseUtil.close(ps);88: DatabaseUtil.close(c);89: }90: }91:
92: publicstaticvoid main(finalString args[])throwsSQLException{93: logger.info("Version 1");94:
95: final JdbcExample e = new JdbcExample();96: e.installSchema();97: e.populateTables();98: e.performSearch();99: }100: }
Using JDBC Directly
This is the base example. In it we:This is a recurring theme through all of the examples. The only real difference is how we design our code to hit JDBC.
Please see the notes on this example below the code.
Class Relationships
Important Note
All of these examples are configured to use hsqldb which is a free database that supports both in-memory operation as well as using the disk (and other configurations). I've set it up to work strictly in memory. This means:Since this is NOT a JDBC example but rather I use JDBC as a vehicle to better understand the design behind the Spring templates, I figure this is a reasonable solution.
JdbcExample.java
Interesting Lines
<--Back Next-->